package com.fenghongzhang.broadcasttest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main6Activity extends AppCompatActivity {
    private Button send6;

    private MyReceiver2 myReceiver2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);


        send6 = (Button) findViewById(R.id.send6);

        //动态注册接收者
        myReceiver2 = new MyReceiver2();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.feng");
        registerReceiver(myReceiver2,intentFilter);


        send6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //发送广播
                //设置频道
                Intent intent = new Intent();
                intent.setAction("com.feng");

                //加数据
                Bundle bundle = new Bundle();
                bundle.putString("name","呵呵呵呵");
                intent.putExtras(bundle);

                sendBroadcast(intent);
            }
        });

    }
//
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver2);
    }
}
